package com.zmn.oms.zmn.normal.business.impl.work;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.remark.OrderRemarkDetailDTO;
import com.zmn.oms.model.dto.work.modify.ZsSyncAccountDIO;
import com.zmn.oms.model.dto.work.modify.must.ZsCancelDTO;
import com.zmn.oms.model.entity.work.OrderWorkQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.model.vo.work.modify.must.ZsCancelVO;
import com.zmn.oms.zmn.normal.business.interfaces.work.OrderLifeCycleBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.ZsNormalWorkBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.financework.ZsNormalErpFinanceWorkBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author sunlife
 * @date:2021/1/28 11:05 上午
 * description: 订单生命周期业务实现类
 */
@Service
@Slf4j
public class OrderLifeCycleBServiceImpl implements OrderLifeCycleBService {

    private static final Integer CANCEL_REASON_ID = 82;
    private static final String CANCEL_REASON_NAME = "遗留订单超最大天数";

    @Resource
    OrderWorkListBService orderWorkListBService;
    @Resource
    ZsNormalErpFinanceWorkBService zsNormalErpFinanceWorkBService;
    @Resource
    ZsNormalWorkBService zsNormalWorkBService;

    @Override
    public void processExpiredOrderWorks(List<Long> workIds) throws OmsBaseException {
        OrderWorkQuery query = new OrderWorkQuery();
        query.setOrderIdList(workIds);
        query.setPageSize(10000);

        query.setIncludes(Lists.newArrayList("id", "orderId", "status", "resultStatus", "completeTime"));

        List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);

        for (EsOrderWork esOrderWork : esOrderWorks) {
            try {
                this.process(esOrderWork);
            } catch (Exception e) {
                log.error(String.format("过期工单自动处理失败[%s]", esOrderWork.getId()), e);
            }
        }

    }

    private void process(EsOrderWork esOrderWork) throws Exception {
        if (esOrderWork.getStatus() < OrderStatusConsts.WORK_STATUS_COMPLETE
                && Objects.equals(esOrderWork.getResultStatus(), OrderStatusConsts.WORK_RESULT_DOING)
                && Objects.isNull(esOrderWork.getCompleteTime())) {

            ZsCancelVO cancelVO = zsNormalWorkBService.getCancelVO(esOrderWork.getOrderId(), esOrderWork.getId());

            // 未完成 取消
            ZsCancelDTO cancelDTO = new ZsCancelDTO();
            cancelDTO.setOrderId(esOrderWork.getOrderId());
            cancelDTO.setWorkId(esOrderWork.getId());
            cancelDTO.setDetailDTOList(Lists.newArrayList(new OrderRemarkDetailDTO(CANCEL_REASON_ID, CANCEL_REASON_NAME, null, null)));
            cancelDTO.setFactoryPart(cancelVO.getFactoryPart());
            cancelDTO.setOperator(GlobalConsts.getOperatorTypeName(GlobalConsts.OPERATE_USER_TYPE_SYSTEM));
            cancelDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            cancelDTO.setOperatorId((long) GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            zsNormalWorkBService.saveCancel(cancelDTO);

        } else if (Objects.equals(esOrderWork.getStatus(), OrderStatusConsts.WORK_STATUS_COMPLETE) || Objects.equals(esOrderWork.getStatus(), OrderStatusConsts.WORK_STATUS_CHECKOUT)){
            // 完成 & 未算账 更新算账状态
            ZsSyncAccountDIO accountDIO = new ZsSyncAccountDIO();
            accountDIO.setOrderId(esOrderWork.getOrderId());
            accountDIO.setWorkId(esOrderWork.getId());
            zsNormalErpFinanceWorkBService.autoAccount(accountDIO);
        }
    }
}
